﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using eService.BLL;
using HLVControl.Grid.Data;
using APP.QUANLY.Common;

namespace eService.Presentation
{
    public partial class usrcDanhMuc_HangHoa : UserControl
    {
        EnumNameForm.ThaoTac thaotac;
        ExcuteHANGHOA exe;
        ExcuteNHOMHANG exeNhomHang;
        ExcuteDONVI exeDV;
        ExcuteNHACUNGCAP exeNcc;

        public usrcDanhMuc_HangHoa()
        {
            InitializeComponent();
        }
        private void usrcDanhMuc_HangHoa_Load(object sender, EventArgs e)
        {
            LoadAll();
        }
        protected virtual void LoadAll()
        {
            LoadConnection();
            LoadUI();
            LoadData();
        }
        private void LoadConnection()
        {
            ConnectionProvider.LoadAllConnection();
        }
        private void LoadUI()
        {
            exe = new ExcuteHANGHOA();
            exeDV = new ExcuteDONVI();
            exeNcc = new ExcuteNHACUNGCAP();
            exeNhomHang = new ExcuteNHOMHANG();
            thaotac = EnumNameForm.ThaoTac.macdinh;
            ControlUI(thaotac);
            UIHelper.LoadFocus(txtTimKiem);
        }
        public void LoadData()
        {
            LoadCmbDonVi();
            LoadCmbNhomHang();
            LoadCmbNcc();
        }
        private void LoadCmbDonVi()
        {
            try
            {
                List<UNITPRODUCT> list = exeDV.GetAll();
                DataTable data = new DataTable();
                data.Columns.Add("Mã", typeof(int));
                data.Columns.Add("Đơn vị", typeof(string));
                foreach (UNITPRODUCT obj in list)
                {
                    data.Rows.Add(obj.UnitID, obj.UnitName);
                }
                cmbDonVi.DataSource = data;
                cmbDonVi.DisplayMember = "Đơn vị";
                cmbDonVi.ValueMember = "Mã";
                cmbDonVi.ColumnWidths = "0,200";
                cmbDonVi.SelectedIndex = -1;
                cmbDonVi.Text = "";
            }
            catch
            {

            }
        }
        private void LoadCmbNhomHang()
        {
            try
            {
                List<GROUPPRODUCT> list = exeNhomHang.GetAll();
                DataTable data = new DataTable();
                data.Columns.Add("Mã", typeof(int));
                data.Columns.Add("Nhóm hàng", typeof(string));
                foreach (GROUPPRODUCT obj in list)
                {
                    data.Rows.Add(obj.GpID, obj.GpName);
                }
                cmbNhom.DataSource = data;
                cmbNhom.DisplayMember = "Nhóm hàng";
                cmbNhom.ValueMember = "Mã";
                cmbNhom.ColumnWidths = "0,200";
                cmbNhom.SelectedIndex = -1;
                cmbNhom.Text = "";
            }
            catch
            {

            }
        }
        private void LoadCmbNcc()
        {
            try
            {
                List<PARTNERINFO> list = exeNcc.GetAll();
                DataTable data = new DataTable();
                data.Columns.Add("Mã", typeof(int));
                data.Columns.Add("Nhà cung cấp", typeof(string));
                foreach (PARTNERINFO obj in list)
                {
                    data.Rows.Add(obj.PartnerID, obj.Name);
                }
                cmbNCC.DataSource = data;
                cmbNCC.DisplayMember = "Nhà cung cấp";
                cmbNCC.ValueMember = "Mã";
                cmbNCC.ColumnWidths = "0,200";
                cmbNCC.SelectedIndex = -1;
                cmbNCC.Text = "";
            }
            catch
            {

            }
        }

        private void ControlUI(EnumNameForm.ThaoTac thaotac)
        {
            if (thaotac == EnumNameForm.ThaoTac.them || thaotac == EnumNameForm.ThaoTac.sua)
            {
                SetStateButton(false);
                if (thaotac == EnumNameForm.ThaoTac.them)
                    LoadValuesToUI(null);
                SetTextReadOnly(false);
                UIHelper.LoadFocus(txtTen);
            }
            if (thaotac == EnumNameForm.ThaoTac.macdinh || thaotac == EnumNameForm.ThaoTac.xoa)
            {
                SetStateButton(true);
                LoadValuesToUI(null);
                SetTextReadOnly(true);

                //Load TreeView
                FillTreeView();
                treeview.RefreshGrouping();

                txtTimKiem.Text = "";
                UIHelper.LoadFocus(txtTimKiem);
            }
        }
        private void SetStateButton(bool IsEnabled)
        {
            btnSua.Enabled = IsEnabled;
            btnThem.Enabled = IsEnabled;
            btnXoa.Enabled = IsEnabled;
            btnXacNhan.Visible = !IsEnabled;
            btnHuy.Visible = !IsEnabled;
            treeview.Enabled = IsEnabled;
            btnTimKiem.Enabled = IsEnabled;
        }
        private void SetTextReadOnly(bool IsReadOnly)
        {
            txtTen.ReadOnly = IsReadOnly;
            txtGhiChu.ReadOnly = IsReadOnly;
            cmbDonVi.Enabled = !IsReadOnly;
            cmbNhom.Enabled = !IsReadOnly;
            cmbTheLoai.Enabled = !IsReadOnly;
            cmbNCC.Enabled = !IsReadOnly;
            txtGiaBan.Enabled = !IsReadOnly;
            txtGiaDacBiet.Enabled = !IsReadOnly;
            txtGiamGia.Enabled = !IsReadOnly;
            txtGiaNhap.Enabled = !IsReadOnly;
            txtVAT.Enabled = !IsReadOnly;
            cbSuDung.Enabled = !IsReadOnly;

        }

        private void LoadValuesToUI(PRODUCT obj)
        {
            if (obj == null)
            {
                obj = new PRODUCT();
                obj.GpID = 0;
                obj.PrUnitID = 0;
                obj.GpParentID = 0;
                obj.TypeProID = 2;
                obj.IsUse = true;
                obj.PrPriceBase = 0;
                obj.PrPriceRetail = 0;
                obj.PrPriceTrade = 0;
                obj.GTGT = 0;
                obj.PrDiscount = 0;
            }
            labMa.Text = obj.PrID.ToString(); 
            txtTen.Text = obj.PrName;
            txtGiaBan.Value = (Decimal)obj.PrPriceRetail;
            txtGiaDacBiet.Value = (Decimal)obj.PrPriceTrade;
            txtGiamGia.Value = (Decimal)obj.PrDiscount;
            txtGiaNhap.Value = (Decimal)obj.PrPriceBase;
            txtVAT.Value = (Decimal)obj.GTGT;
            labMaDonVi.Text = obj.PrUnitID.ToString();
            labMaNhom.Text = obj.GpID.ToString();
            labNCC.Text = obj.GpParentID.ToString();
            cbSuDung.Checked = (bool)obj.IsUse;
            txtGhiChu.Text = obj.PrNotes;

          
            GROUPPRODUCT objGrp = exeNhomHang.GetByID((int)obj.GpID);
            if (objGrp != null) cmbNhom.Text = objGrp.GpName;
            else cmbNhom.Text = "";
            UNITPRODUCT objDV = exeDV.GetByID((int)obj.PrUnitID);
            if (objDV != null) cmbDonVi.Text = objDV.UnitName;
            else cmbDonVi.Text = "";

            try
            {
                PARTNERINFO objN = exeNcc.GetByID((int)obj.GpParentID);
                if (objN != null) cmbNCC.Text = objN.Name;
                else cmbNCC.Text = "";
            }
            catch { }

            if ((short)obj.TypeProID == 0) cmbTheLoai.Text = "Nguyên vật liệu";
            if ((short)obj.TypeProID == 1) cmbTheLoai.Text = "Thành phẩm";
            if ((short)obj.TypeProID == 2) cmbTheLoai.Text = "Tổng hợp";
        }
        private void FillTreeView()
        {
            List<PRODUCT> data = exe.GetAll();
            exe.LoadToTreeview(treeview,data);
        }
        private bool XuLy(EnumNameForm.ThaoTac thaotac)
        {
            var flag = false;
            try
            {
                short TheLoai=2;
                if(cmbTheLoai.Text=="Tổng hợp")TheLoai=2;
                if(cmbTheLoai.Text=="Thành phẩm")TheLoai=1;
                if(cmbTheLoai.Text=="Nguyên vật liệu")TheLoai=0;

                PRODUCT obj = exe.LoadValues(int.Parse(labMa.Text),int.Parse(labMaNhom.Text),int.Parse(labMaDonVi.Text),int.Parse(labNCC.Text),txtTen.Text,TheLoai,decimal.Parse(txtGiaNhap.Text),decimal.Parse(txtGiaBan.Text),decimal.Parse(txtGiaDacBiet.Text),decimal.Parse(txtGiamGia.Text),int.Parse(txtVAT.Text),cbSuDung.Checked,txtGhiChu.Text);
                flag = exe.KQ;
                if (flag)
                {
                    flag = false;
                    switch (thaotac)
                    {
                        case EnumNameForm.ThaoTac.them:
                            flag = exe.Add(obj);
                            break;
                        case EnumNameForm.ThaoTac.sua:
                            flag = exe.UpDate(obj);
                            break;
                        case EnumNameForm.ThaoTac.xoa:
                            flag = exe.Delete(obj);
                            break;
                    }
                    flag = exe.KQ;
                }
                MessageBox.Show(exe.ErrMsg, "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch
            {
                flag = false;
            }
            return flag;
        }
        private void button_Click(object sender, EventArgs e)
        {
            bool KQ = false;
            Button btn = (Button)sender;
            string _thaotac = btn.Tag.ToString();
            if ((_thaotac == "sua" && labMa.Text == "0") || (_thaotac == "xoa" && labMa.Text == "0"))
            {
                MessageBox.Show("Vui lòng chọn mặt hàng!");
                return;
            }
            switch (_thaotac)
            {
                case "them":
                    thaotac = EnumNameForm.ThaoTac.them;
                    break;
                case "sua":
                    thaotac = EnumNameForm.ThaoTac.sua;
                    break;
                case "huy":
                    thaotac = EnumNameForm.ThaoTac.macdinh;
                    break;
                case "xoa":
                    KQ = XuLy(EnumNameForm.ThaoTac.xoa);
                    if (KQ) thaotac = EnumNameForm.ThaoTac.macdinh;
                    break;
                case "xacnhan":
                    KQ = XuLy(thaotac);
                    if (KQ) thaotac = EnumNameForm.ThaoTac.macdinh;
                    break;
            }
            ControlUI(thaotac);
        }

        private void treeview_AfterSelectionChange(object sender, HLVControl.Grid.Events.TreeListElementEventArgs e)
        {
            try
            {
                TreeListRow firstSelected = treeview.SelectedElements[0] as TreeListRow;
                int ID = int.Parse(firstSelected.Cells["colID"].Value.ToString());
                PRODUCT obj = exe.GetByID(ID);
                if (obj == null) return;
                LoadValuesToUI(obj);
                txtTen.Focus();
            }
            catch
            {

            }
        }
        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            SearchProvider.SmartSearch(treeview, txtTimKiem.Text.Trim(), new List<string> {  });
        }

        private void txt_Enter(object sender, EventArgs e)
        {
            UIHelper.txt_Enter(sender, e);
        }
        private void txt_Leave(object sender, EventArgs e)
        {
            UIHelper.txt_Leave(sender, e);
        }
        private void txt_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                TextBox txt = sender as TextBox;
                if (txt.Name == txtTen.Name)
                    UIHelper.txt_KeyPress(sender, cmbDonVi, e);
                if (txt.Name == cmbDonVi.Name)
                    UIHelper.txt_KeyPress(sender, cmbNhom, e);
                if (txt.Name == cmbNhom.Name)
                    UIHelper.txt_KeyPress(sender, cmbTheLoai, e);
                if (txt.Name == cmbTheLoai.Name)
                    UIHelper.txt_KeyPress(sender, cmbNCC, e);
                if (txt.Name == cmbNCC.Name)
                    UIHelper.txt_KeyPress(sender, txtGhiChu, e);
                if (txt.Name == txtGhiChu.Name)
                    UIHelper.txt_KeyPress(sender, txtGiaNhap, e);
                if (txt.Name == txtGiaNhap.Name)
                    UIHelper.txt_KeyPress(sender, txtGiaBan, e);
                if (txt.Name == txtGiaBan.Name)
                    UIHelper.txt_KeyPress(sender, txtGiaDacBiet, e);
                if (txt.Name == txtGiaDacBiet.Name)
                    UIHelper.txt_KeyPress(sender, txtGiamGia, e);
                if (txt.Name == txtGiamGia.Name)
                    UIHelper.txt_KeyPress(sender, txtVAT, e);
                if (txt.Name == txtVAT.Name)
                    UIHelper.txt_KeyPress(sender, btnXacNhan, e);
                if (txt.Name == txtTimKiem.Name)
                    UIHelper.txt_KeyPress(sender, btnTimKiem, e);
            }
            catch { }
        }

        private void cmbDonVi_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbDonVi.SelectedIndex >= 0)
            {
                DataRowView drvData = (DataRowView)cmbDonVi.SelectedItem;
                string Ma = drvData.Row.ItemArray[0].ToString();
                string Ten = drvData.Row.ItemArray[1].ToString();
                labMaDonVi.Text = Ma;
                cmbDonVi.Text = Ten;
            }
        }
        private void cmbNhom_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbNhom.SelectedIndex >= 0)
            {
                DataRowView drvData = (DataRowView)cmbNhom.SelectedItem;
                string Ma = drvData.Row.ItemArray[0].ToString();
                string Ten = drvData.Row.ItemArray[1].ToString();
                labMaNhom.Text = Ma;
                cmbNhom.Text = Ten;
            }
        }
        private void cmbNCC_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbNCC.SelectedIndex >= 0)
            {
                DataRowView drvData = (DataRowView)cmbNCC.SelectedItem;
                string Ma = drvData.Row.ItemArray[0].ToString();
                string Ten = drvData.Row.ItemArray[1].ToString();
                labNCC.Text = Ma;
                cmbNCC.Text = Ten;
            }
        }
    }
}
